package com.ody.util.code.config;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class DatabaseConfigPanel extends JPanel implements IConfigPanel {
	private static final long serialVersionUID = 5471235329179897180L;
	
	private JTextField hostText;
	private JTextField portText;
	private JTextField usrText;
	private JTextField pwdText;
	private JTextField dbText;
	private JTextField tableText;
	private JTextArea colText;
	
	public DatabaseConfigPanel(ConfigProperties configProperties, final IConfigLog log) throws Exception {
		Properties props = configProperties.getProps();
		String host = props.getProperty("host"); 
		String port = props.getProperty("port");
		String username = props.getProperty("username");
		String password = props.getProperty("password");
		String db = props.getProperty("db");
		String tablePattern = props.getProperty("tablePattern");
		String columnPattern = props.getProperty("columnPattern");
		
	    GridBagLayout layout = new GridBagLayout();
	    GridBagConstraints gc = new GridBagConstraints();
		gc.fill = GridBagConstraints.BOTH;
	    
	    this.setLayout(layout);

		JLabel hostLabel = new JLabel("主机名：", SwingConstants.RIGHT);
		hostText = new JTextField(host, 20);
	    
		JLabel portLabel = new JLabel("端口：", SwingConstants.RIGHT);
		portText = new JTextField(port, 5);

		JLabel usrLabel = new JLabel("用户名：", SwingConstants.RIGHT);
		usrText = new JTextField(username, 20);

		JLabel pwdLabel = new JLabel("密码：", SwingConstants.RIGHT);
		pwdText = new JPasswordField(password, 20);

		JLabel dbLabel = new JLabel("数据库： ", SwingConstants.RIGHT);
		dbText = new JTextField(db, 45);

		JLabel tableLabel = new JLabel("表名过滤： ", SwingConstants.RIGHT);
		tableText = new JTextField(tablePattern, 45);

		JLabel colLabel = new JLabel("列名过滤： ", SwingConstants.RIGHT);
		colText = new JTextArea(columnPattern);
		colText.setRows(5);
		colText.setLineWrap(true);
	    JScrollPane colTextPanel = new JScrollPane(colText);
	    
		JButton test = new JButton("测试");
		test.addActionListener(t -> {
			new Thread(() -> {
				try {
					log.write("测试中..");
					IJdbcConfig config = getJdbcConfig();
					Class.forName(config.getJdbcDriver());
					Connection conn = DriverManager.getConnection(config.getJdbcUrl(), config.getJdbcUsername(), config.getJdbcPassword());
					conn.close();
					log.write("测试成功");
				} catch (Exception e) {
					log.write(e.getMessage());
				}
			}).start();
		});
		
	    gc.gridwidth = 1;
		layout.addLayoutComponent(hostLabel, gc);
	    layout.addLayoutComponent(hostText, gc);
	    layout.addLayoutComponent(portLabel, gc);
	    gc.gridwidth = GridBagConstraints.REMAINDER;
	    layout.addLayoutComponent(portText, gc);

	    gc.gridwidth = 1;
	    layout.addLayoutComponent(usrLabel, gc);
	    layout.addLayoutComponent(usrText, gc);
	    layout.addLayoutComponent(pwdLabel, gc);
	    gc.gridwidth = GridBagConstraints.REMAINDER;
	    layout.addLayoutComponent(pwdText, gc);

	    gc.gridwidth = 1;
	    layout.addLayoutComponent(dbLabel, gc);
	    gc.gridwidth = GridBagConstraints.REMAINDER;
	    layout.addLayoutComponent(dbText, gc);

	    gc.gridwidth = 1;
	    layout.addLayoutComponent(tableLabel, gc);
	    gc.gridwidth = GridBagConstraints.REMAINDER;
	    layout.addLayoutComponent(tableText, gc);

	    gc.gridwidth = 1;
	    layout.addLayoutComponent(colLabel, gc);
	    gc.gridwidth = GridBagConstraints.REMAINDER;
	    layout.addLayoutComponent(colTextPanel, gc);

	    gc.weighty = 2;
	    layout.addLayoutComponent(test, gc);
	    JSeparator bottomSpt = new JSeparator(SwingConstants.HORIZONTAL);
	    layout.addLayoutComponent(bottomSpt, gc);
	    
		this.add(hostLabel);
		this.add(hostText);
		this.add(portLabel);
		this.add(portText);
		this.add(usrLabel);
		this.add(usrText);
		this.add(pwdLabel);
		this.add(pwdText);
		this.add(dbLabel);
		this.add(dbText);
		this.add(tableLabel);
		this.add(tableText);
		this.add(colLabel);
		this.add(colTextPanel);
		this.add(test);
		this.add(bottomSpt);
	}

	@Override
	public Properties getConfig() {
		Properties cfgs = new Properties();
		cfgs.setProperty("host", hostText.getText());
		cfgs.setProperty("port", portText.getText());
		cfgs.setProperty("username", usrText.getText());
		cfgs.setProperty("password", pwdText.getText());
		cfgs.setProperty("db", dbText.getText());
		cfgs.setProperty("tablePattern", tableText.getText());
		cfgs.setProperty("columnPattern", colText.getText());
		
		IJdbcConfig jdbcCfg = getJdbcConfig();
		cfgs.setProperty("jdbc.driver", jdbcCfg.getJdbcDriver());
		cfgs.setProperty("jdbc.url", jdbcCfg.getJdbcUrl());
		cfgs.setProperty("jdbc.username", jdbcCfg.getJdbcUsername());
		cfgs.setProperty("jdbc.password", jdbcCfg.getJdbcPassword());
		
		return cfgs;
	}
	
	private IJdbcConfig getJdbcConfig() {
		return new MysqlJdbcConfig(hostText.getText(), Integer.parseInt(portText.getText()), usrText.getText(), pwdText.getText(), dbText.getText());
	}

	@Override
	public void onWindowOpened() {
		tableText.requestFocus();
	}

	@Override
	public void onWindowClosed() {}
	
}
